﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DFYE.Web.Areas.powerManage.Controllers
{
    using DFYE.Web.Models;
    using JULONG.WEB.Library;
    public class channelController : Controller
    {
        DataSession<entityDBContainer, channel> dbSession = new DataSession<entityDBContainer, channel>();

        //
        // GET: /manage/channel/
        public channelController()
        {
            ViewBag.parents = dbSession.query().Where(d => d.parentID == null).ToList();
        }

        public ActionResult Index()
        {
            ViewData.Model = dbSession.query().Where(d => d.parentID == null);
            return View();
        }


        //
        // GET: /manage/channel/Create
        public ActionResult Create(int? id) //id 为父频道ID
        {
            ViewData["parentID"] = id;
            var model = dbSession.create();
            //powerPerfect(ref model); //完善权限
            ViewData.Model = model;
            return View();
        }

        //
        // POST: /manage/channel/Create
        [HttpPost]
        public ActionResult Create(channel channel, FormCollection fc)
        {
            try
            {
                //channel.postCount = 0;
                channel.createDate = DateTime.Now;
                //powerListSet(ref channel, fc); //设置权限
                dbSession.insert(channel);
                //manageLog.add(manageActionTypeEnum.后台管理, Request.UserHostAddress + " 创建频道:" + channel.name, channel.ID);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ViewData.Model = channel;
                return View();
            }
        }


        // GET: /manage/channel/Edit/5
        public ActionResult Edit(int id)
        {
            var model = dbSession.get(d => d.ID == id);
            //powerPerfect(ref model);
            ViewData.Model = model;
            ViewData["parents"] = dbSession.query().Where(d => d.parentID == null).ToList();
            return View();
        }

        //
        // POST: /manage/channel/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection fc)
        {
            try
            {
                channel channel = dbSession.get(d => d.ID == id);
                TryUpdateModel(channel);
                //powerListSet(ref channel, fc); //设置权限
                dbSession.save();
                //manageLog.add(manageActionTypeEnum.后台管理, Request.UserHostAddress + " 编辑频道:" + channel.name, channel.ID);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /manage/channel/Delete/5
        public ActionResult Delete(int id)
        {
            ViewData.Model = dbSession.get(d => d.ID == id);
            return View();
        }

        //
        // POST: /manage/channel/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var cnl = dbSession.get(d => d.ID == id);

            if (cnl.requirement)
            {
                //myError error = new myError("频道删除出错", "唯一频道不可以删除");
                ModelState.AddModelError("", "频道删除出错,系统设定为不可删除");
                return View(cnl);
            }
            
            if (cnl.channel_sub.Any())
            {
                //myError error = new myError("频道删除出错", cnl.channel_sub.Select(d => d.name).ToArray());
                ModelState.AddModelError("", "频道删除出错,有子频道");
                return View(cnl);
            }
            else
            {
                //manageLog.add(manageActionTypeEnum.后台管理, Request.UserHostAddress + " 删除频道:" + cnl.name, cnl.ID);
                //cnl.post.Load();
                dbSession.delete(cnl);
                return RedirectToAction("index");
            }
        }


    }
}
